package cn.beatuty.makeup.module.system.controller.admin.follow;

import cn.beatuty.makeup.module.system.controller.admin.follow.vo.FollowPageReqVO;
import cn.beatuty.makeup.module.system.controller.admin.follow.vo.FollowRespVO;
import cn.beatuty.makeup.module.system.controller.admin.follow.vo.FollowSaveReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.follow.FollowDO;
import cn.beatuty.makeup.module.system.service.follow.FollowService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.beatuty.makeup.framework.common.pojo.PageParam;
import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.common.pojo.CommonResult;
import cn.beatuty.makeup.framework.common.util.object.BeanUtils;
import static cn.beatuty.makeup.framework.common.pojo.CommonResult.success;

import cn.beatuty.makeup.framework.excel.core.util.ExcelUtils;

import cn.beatuty.makeup.framework.operatelog.core.annotations.OperateLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.beatuty.makeup.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 用户关注关系")
@RestController
@RequestMapping("/app/follow")
@Validated
public class FollowController {

    @Resource
    private FollowService followService;

    @PostMapping("/create")
    @Operation(summary = "创建用户关注关系")
    public CommonResult<Long> createFollow(@Valid @RequestBody FollowSaveReqVO createReqVO) {
        return success(followService.createFollow(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户关注关系")
    public CommonResult<Boolean> updateFollow(@Valid @RequestBody FollowSaveReqVO updateReqVO) {
        followService.updateFollow(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户关注关系")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteFollow(@RequestParam("id") Long id) {
        followService.deleteFollow(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户关注关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<FollowRespVO> getFollow(@RequestParam("id") Long id) {
        FollowDO follow = followService.getFollow(id);
        return success(BeanUtils.toBean(follow, FollowRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得用户关注关系分页")
    public CommonResult<PageResult<FollowRespVO>> getFollowPage(@Valid @RequestBody FollowPageReqVO pageReqVO) {
        if (pageReqVO.getIsAllData()){
            pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        }
        PageResult<FollowDO> pageResult = followService.getFollowPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, FollowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户关注关系 Excel")
    @OperateLog(type = EXPORT)
    public void exportFollowExcel(@Valid FollowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<FollowDO> list = followService.getFollowPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户关注关系.xls", "数据", FollowRespVO.class,
                        BeanUtils.toBean(list, FollowRespVO.class));
    }

}